package 力扣日常刷题.木22二月.第21天227;

/**
 * @author 帅小伙
 * @date 2022/2/27
 * @description
 */
public class Demo174在排序数组中查找元素的第一个和最后一个位置 {

    public int[] searchRange(int[] nums, int target) {
        int n = nums.length;
        if(n == 0) return new int[]{-1,-1};
        int l = 0, r = n - 1;

        // 找左边界
        while (l < r) {
            int mid = (l + r) >> 1;
            if (nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        if (nums[l] != target) return new int[]{-1, -1};
        int k = l;
        r = n - 1;
        // 找右边界
        while (l < r) {
            int mid = (l + r + 1) >> 1;
            if (nums[mid] <= target) l = mid;
            else r = mid - 1;
        }
        return new int[]{k,r};
    }
}
